package cn.jekun.shop.app.action;

import javax.annotation.Resource;

import net.sf.json.JSONObject;
import cn.jekun.component.base.bean.ErrorMsg;
import cn.jekun.component.base.exceptions.BusinessException;
import cn.jekun.shop.common.service.CreditsDailySignService;

/**
 * 每日签到
 * @author yuan
 *
 */
public class CreditsDailySignAction extends AppBaseAction {
	
	@Resource(name=CreditsDailySignService.BEAN_NAME)
	CreditsDailySignService creditsDailySignService;
	/**
	 * 每日签到日志(同时返回当天是否已经签到)
	 * @return
	 */
	public String getSignLog(){
		JSONObject jsonObject = null;
		try{
			
			String pageSize = this.request.getParameter("pageSize");
			String currentPage = this.request.getParameter("currentPage");
//			String userId = "201404021748370000004";
			String userId = this.getUserId();
			
			this.validateSign(getLoginSign(),getUserId());//验证签名:loginSign+userId
			this.validateLoginApp();//验证是否登录
			
			int ps=10,p=1;
			try {
				ps = Integer.parseInt(pageSize);
			} catch (Exception e) {
			}
			try {
				p = Integer.parseInt(currentPage);
			} catch (Exception e) {
			}
			page.setPageSize(ps);
			page.setPageNumber(p);
			
			jsonObject = creditsDailySignService.getSignLog(userId,page);
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	/**
	 * 每日签到，获取积分
	 * @return
	 */
	public String dailySign(){
		JSONObject jsonObject = null;
		try{
			
//			String userId = "201404081116180000017";
			String userId = this.getUserId();
			
			this.validateSign(getLoginSign(),getUserId());//验证签名:loginSign+userId
			this.validateLoginApp();//验证是否登录
			
			jsonObject = creditsDailySignService.dailySign(userId);
			this.logService.addMallLog(this.getUserId(),this.getBrowser(), this.getIp(), "手机每日签到");//记录操作日志
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	
}
